package com.gome.ss.flowcrawl.extractor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;


public class HTMLFilter 
{
	public  HashMap<String, HashMap<String, Boolean>> filtDict = new HashMap<String, HashMap<String, Boolean>>();
	private String filtName = "";
	public HTMLFilter(String filtName)
	{
		this.filtName = filtName;
	}
	
	public  void loadDict()
	{
		try
		{
			File file = new File(filtName);
			if (!file.exists())
				return;
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8") );
            String s="";
            String line;
        
            while ( (line = reader.readLine()) != null)
            {
            	String[] p = line.split("\t");
            
            	if(p.length!=3 || Integer.parseInt(p[2])<3)
            		continue;
     			
            	if(filtDict.containsKey(p[1]))
            	{
            		filtDict.get(p[1]).put(p[0], true);
            	}
            	else
            	{
            		HashMap<String, Boolean> m = new HashMap<String, Boolean>();
            		m.put(p[0], true);
            		filtDict.put(p[1], m);
            	}
            }
            reader.close();
        }
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public  void removeNode(Element e)
	{
		Elements es  = e.children();
		String tagName = "";
		String text = "";
	
			tagName = e.tagName();
			text = e.text();
//			System.out.println(tagName + "\t" + text);
			String href = e.attr("href");
			String src = e.attr("src");
			String url = e.attr("url");
			
			if(href.length()!=0 && filtDict.containsKey(href) && filtDict.get(href).containsKey(tagName))
			{
				e.removeAttr("href");
			}
			if(src.length()!=0 &&  filtDict.containsKey(src) && filtDict.get(src).containsKey(tagName))
			{
				e.removeAttr("src");
			}
			if(url.length()!=0 && filtDict.containsKey(url) && filtDict.get(url).containsKey(tagName))
			{
				e.removeAttr("url");
			}
			if(text.length()!=0 && filtDict.containsKey(text) && filtDict.get(text).containsKey(tagName))
			{
				e.remove();
			}
		
			else
			{
				for(Element en : es)
				{
					removeNode(en);
				}
			}
	}
	
	public  String filt(String input)
	{
		String result = "";
		Document doc = Jsoup.parse(input);
		Elements e = doc.getElementsByTag("body");

		removeNode(e.get(0));

		result = doc.toString();		
		return result;

	}
}
